package de.gist.calldurationwatcher.helper;

import java.util.Date;
import de.gist.calldurationwatcher.R;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class CallLogListAdapter extends SimpleCursorAdapter {

	public CallLogListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
		
		super(context, layout, c, from, to);
	}
	
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		
		try {
			
			Date callTime = new Date(cursor.getLong(CallLogFields.DATE.ordinal()));
            String name = cursor.getString(CallLogFields.NAME.ordinal());
            long duration = cursor.getLong(CallLogFields.DURATION.ordinal());
            int minutes = Utils.GetMinutes(duration);
            String number = cursor.getString(CallLogFields.NUMBER.ordinal());
            
            TextView txtName = (TextView) view.findViewById(R.id.txtName);
            txtName.setText(name);
            
            TextView txtDate = (TextView) view.findViewById(R.id.txtDate);
            txtDate.setText(callTime.toLocaleString());
            
            TextView txtPhone = (TextView) view.findViewById(R.id.txtNumber);
            //if (name != null && name.length() > 0)
            //	txtPhone.setText(name);
            //else
            	txtPhone.setText(number);
            
            TextView txtDuration = (TextView) view.findViewById(R.id.txtDuration);
            txtDuration.setText(minutes + " min");
			
		} catch (Exception ex) {
			
		}
	}
}
